package com.smart.service.push;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.smart.manager.member.entities.models.MemberProfileMain;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@Log4j2
public class PushService {

    /**
     * 推送系统通知
     */
    public void pushSystemNotice (MemberProfileMain member,String msg){

        templateMessages(List.of("13065ffa4f0abb564f1"),msg);

    }

    public static void main(String[] args) {

//        System.out.println("args = " + );

//        templateMessages(List.of("13065ffa4f0abb564f1"),"踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩从");
    }


    /**
     * 单点模板消息
     * 通过registration_id来推送
     */
    public static void templateMessages(List<String> member,String msg){
        String push = "https://api.jpush.cn/v3/push";
        //DevKey:DevSecret
        String key = Base64.encode("5925892c4e030bc18ba99b9c:3c7c2b22c924a04d463509eb");
        PushBody body = new PushBody()
                .setPlatform("all")
                .setAudience(new PushBody.Audience().setRegistration_id(member))
                .setNotification(new PushBody.Notification().setAlert(msg));
        String toString = new JSONObject(body).toString();
        String query = HttpRequest.post(push).header("Authorization", "Basic "+ key).body(toString).execute().body();
        String msgId = JSONUtil.parse(query).getByPath("sendno",String.class);
        if (msgId.equals("0")){
            log.warn("推送成功");
        }else {
            log.warn(query);
        }
    }
}
